package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.WidgetVipLevelBinding

/**
 * VIp等级控件
 *
 * @author Created by 天晴 on 2024/12/19 10:41
 */
class VipLevelView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetVipLevelBinding.inflate(LayoutInflater.from(context), this)
    private val vipLabels = arrayOf(
        R.drawable.vip_label_1, // 对应等级 0
        R.drawable.vip_label_2, // 对应等级 1
        R.drawable.vip_label_3, // 对应等级 2
        R.drawable.vip_label_4, // 对应等级 3
        R.drawable.vip_label_5, // 对应等级 4
        R.drawable.vip_label_6  // 对应等级 5
    )

    private fun isNumberInRange(number: Long): Boolean {
        return number in 1..6
    }

    /**
     * 设置等级
     */
    fun setVipLevel(level: Long?, width: Int = 55.autoDp, height: Int = 18.autoDp) {
        if (level == null || !isNumberInRange(level)) {
            visibility = GONE
            return
        }
        val lp = mBinding.bg.layoutParams
        lp.width = width
        lp.height = height
        mBinding.bg.layoutParams = lp
        mBinding.bg.setImageResource(vipLabels[level.toInt() - 1])
        visibility = VISIBLE
    }
}